package com.tools;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.toedter.calendar.JDateChooser;

@SuppressWarnings("serial")
public class BICCelluleDate extends JTextField {
	@SuppressWarnings("unused")
	private JDateChooser datechooser;
	@SuppressWarnings("unused")
	private JButton bouton;
	private JFrame frame;

	public BICCelluleDate() {
		super();
		datechooser = new JDateChooser();
		bouton = new JButton("Valider");
		this.addMouseListener(new CellDateListener());
		this.setEditable(false);
		frame = new JFrame();
		frame.setAlwaysOnTop(true);
	}
	
	public BICCelluleDate(int taille) {
		super(taille);
		datechooser = new JDateChooser();
		bouton = new JButton("Valider");
		this.addMouseListener(new CellDateListener());
		this.setEditable(false);
		frame = new JFrame();
		frame.setAlwaysOnTop(true);
	}

	public JTextField getTextField() {
		return this;
	}

	class CellDateListener extends MouseAdapter {
		public void mousePressed(MouseEvent source) {
			final JButton bouton = new JButton("Valider");
			final JDateChooser dateChooser = new JDateChooser();
			dateChooser.setLocale(Locale.FRANCE);
			frame.setLayout(new BorderLayout());
			frame.add(dateChooser, BorderLayout.CENTER);
			frame.add(bouton, BorderLayout.SOUTH);
			bouton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					if (dateChooser.getDate() != null) {
						Date date = dateChooser.getDate();
						SimpleDateFormat simpleFormat = new SimpleDateFormat(
								"yyyy-MM-dd");
						getTextField().setText("" + simpleFormat.format(date));
						frame.dispose();
					} else {
						JOptionPane.showMessageDialog(
								(JButton) event.getSource(),
								"Choisissez une date svp !");
					}
				}
			});
			frame.setLocationRelativeTo(getTextField());
			frame.setResizable(false);
			frame.setPreferredSize(new Dimension(170, 80));
			frame.pack();
			frame.setVisible(true);
		}
		// public void mouseEntered(MouseEvent source) {}
		// public void mouseClicked(MouseEvent arg0) {}
		// public void mouseExited(MouseEvent arg0) {}
		// public void mousePressed(MouseEvent arg0) {}
		// public void mouseReleased(MouseEvent arg0) {}
	}
}
